﻿using System;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Collections;

namespace SPFoundation
{
    /// <summary>
    /// http://msdn.microsoft.com/en-us/library/gg981880.aspx
    /// </summary>
    public class EventReceiverLogging
    {
        private void LogWebEventProperties(SPWebEventProperties properties)
        {
            // Specify the log list name.
            string listName = "WebEventLogger";

            // Create string builder object.
            StringBuilder sb = new StringBuilder();

            // Add properties that do not throw exceptions.
            sb.AppendFormat("Cancel: {0}\n", properties.Cancel);
            sb.AppendFormat("ErrorMessage: {0}\n", properties.ErrorMessage);
            sb.AppendFormat("EventType: {0}\n", properties.EventType);
            sb.AppendFormat("FullUrl: {0}\n", properties.FullUrl);
            sb.AppendFormat("NewServerRelativeUrl: {0}\n", properties.NewServerRelativeUrl);
            sb.AppendFormat("ParentWebId: {0}\n", properties.ParentWebId);
            sb.AppendFormat("ReceiverData: {0}\n", properties.ReceiverData);
            sb.AppendFormat("RedirectUrl: {0}\n", properties.RedirectUrl);
            sb.AppendFormat("ServerRelativeUrl: {0}\n", properties.ServerRelativeUrl);
            sb.AppendFormat("SiteId: {0}\n", properties.SiteId);
            sb.AppendFormat("Status: {0}\n", properties.Status);
            sb.AppendFormat("UserDisplayName: {0}\n", properties.UserDisplayName);
            sb.AppendFormat("UserLoginName: {0}\n", properties.UserLoginName);
            sb.AppendFormat("WebId: {0}\n", properties.WebId);
            sb.AppendFormat("Web: {0}\n", properties.Web);

            // Log the event to the list.

            ListBaseLogging.LogEvent(properties.Web, listName, properties.EventType, sb.ToString());
           
        }

        private void LogListEventProperties(SPListEventProperties properties)
        {
            // Specify the log list name.
            string listName = "ListEventLogger";

            // Create the string builder object.
            StringBuilder sb = new StringBuilder();

            // Add properties that do not throw exceptions.
            sb.AppendFormat("Cancel: {0}\n", properties.Cancel);
            sb.AppendFormat("ErrorMessage: {0}\n", properties.ErrorMessage);
            sb.AppendFormat("EventType: {0}\n", properties.EventType);
            sb.AppendFormat("FeatureId: {0}\n", properties.FeatureId);
            sb.AppendFormat("FieldName: {0}\n", properties.FieldName);
            sb.AppendFormat("FieldXml: {0}\n", properties.FieldXml);
            sb.AppendFormat("ListId: {0}\n", properties.ListId);
            sb.AppendFormat("ListTitle: {0}\n", properties.ListTitle);
            sb.AppendFormat("ReceiverData: {0}\n", properties.ReceiverData);
            sb.AppendFormat("RedirectUrl: {0}\n", properties.RedirectUrl);
            sb.AppendFormat("SiteId: {0}\n", properties.SiteId);
            sb.AppendFormat("Status: {0}\n", properties.Status);
            sb.AppendFormat("TemplateId: {0}\n", properties.TemplateId);
            sb.AppendFormat("UserDisplayName: {0}\n", properties.UserDisplayName);
            sb.AppendFormat("UserLoginName: {0}\n", properties.UserLoginName);
            sb.AppendFormat("WebId: {0}\n", properties.WebId);
            sb.AppendFormat("WebUrl: {0}\n", properties.WebUrl);
            sb.AppendFormat("Web: {0}\n", properties.Web);
            sb.AppendFormat("List: {0}\n", properties.List);

            // Add properties that might throw exceptions.
            try
            {
                sb.AppendFormat("Field: {0}\n", properties.Field);
            }
            catch (Exception e)
            {
                sb.AppendFormat("\nError accessing properties.Field:\n\n {0}", e);
            }

            // Log the event to the list.

            ListBaseLogging.LogEvent(properties.Web, listName, properties.EventType, sb.ToString());
           
        }

        public void LogEmailReceivedEventProperties(SPList list, SPEmailMessage emailMessage, String receiverData)
        {
            // Specify the log list name.
            string listName = "ListEmailEventLogger";

            // Create the string builder object.
            StringBuilder sb = new StringBuilder();

            // Add the email message properties.
            sb.AppendFormat("From:\t {0}\n", emailMessage.Sender);
            sb.AppendFormat("To:\t {0}\n", emailMessage.Headers["To"]);
            sb.AppendFormat("Subject:\t {0}\n", emailMessage.Headers["Subject"]);
            sb.AppendFormat("Body:\t {0}\n", emailMessage.PlainTextBody);

            // Log the event to the list.
            ListBaseLogging.LogEvent(list.ParentWeb, listName, SPEventReceiverType.EmailReceived, sb.ToString());
        }

        private void LogItemEventProperties(SPItemEventProperties properties)
        {
            // Specify the log list name.
            string listName = "ListItemEventLogger";

            // Create the string builder object.
            StringBuilder sb = new StringBuilder();

            // Add properties that do not throw exceptions.
            sb.AppendFormat("AfterUrl: {0}\n", properties.AfterUrl);
            sb.AppendFormat("BeforeUrl: {0}\n", properties.BeforeUrl);
            sb.AppendFormat("Cancel: {0}\n", properties.Cancel);
            sb.AppendFormat("CurrentUserId: {0}\n", properties.CurrentUserId);
            sb.AppendFormat("ErrorMessage: {0}\n", properties.ErrorMessage);
            sb.AppendFormat("EventType: {0}\n", properties.EventType);
            sb.AppendFormat("ListId: {0}\n", properties.ListId);
            sb.AppendFormat("ListItemId: {0}\n", properties.ListItemId);
            sb.AppendFormat("ListTitle: {0}\n", properties.ListTitle);
            sb.AppendFormat("ReceiverData: {0}\n", properties.ReceiverData);
            sb.AppendFormat("RedirectUrl: {0}\n", properties.RedirectUrl);
            sb.AppendFormat("RelativeWebUrl: {0}\n", properties.RelativeWebUrl);
            sb.AppendFormat("SiteId: {0}\n", properties.SiteId);
            sb.AppendFormat("Status: {0}\n", properties.Status);
            sb.AppendFormat("UserDisplayName: {0}\n", properties.UserDisplayName);
            sb.AppendFormat("UserLoginName: {0}\n", properties.UserLoginName);
            sb.AppendFormat("Versionless: {0}\n", properties.Versionless);
            sb.AppendFormat("WebUrl: {0}\n", properties.WebUrl);
            sb.AppendFormat("Web: {0}\n", properties.Web);
            sb.AppendFormat("Zone: {0}\n", properties.Zone);
            sb.AppendFormat("Context: {0}\n", properties.Context);

            // Add properties that might throw exceptions.
            try
            {
                sb.AppendFormat("ListItem: {0}\n", properties.ListItem);
            }
            catch (Exception e)
            {
                sb.AppendFormat("\nError accessing properties.ListItem:\n\n {0}", e);
            }

            // Add AfterProperties property bag.
            sb.AppendFormat("AfterProperties: {0}\n", properties.AfterProperties);
            IEnumerator afterProperties = properties.AfterProperties.GetEnumerator();
            int i = 0;
            while (afterProperties.MoveNext())
            {
                DictionaryEntry entry = (DictionaryEntry)afterProperties.Current;
                sb.AppendFormat("[{0}]: ({1}, {2})\n", i++, entry.Key, entry.Value);
            }
            sb.AppendFormat("AfterProperties.ChangedProperties: {0}\n",
              properties.AfterProperties.ChangedProperties);
            IEnumerator changedAfterProperties =
              properties.AfterProperties.ChangedProperties.GetEnumerator();
            i = 0;
            while (changedAfterProperties.MoveNext())
            {
                DictionaryEntry entry = (DictionaryEntry)changedAfterProperties.Current;
                sb.AppendFormat("[{0}]: ({1}, {2})\n", i++, entry.Key, entry.Value);
            }

            // Add BeforeProperties property bag.
            sb.AppendFormat("BeforeProperties: {0}\n", properties.BeforeProperties);
            IEnumerator beforeProperties = properties.BeforeProperties.GetEnumerator();
            i = 0;
            while (beforeProperties.MoveNext())
            {
                DictionaryEntry entry = (DictionaryEntry)beforeProperties.Current;
                sb.AppendFormat("[{0}]: ({1}, {2})\n", i++, entry.Key, entry.Value);
            }
            sb.AppendFormat("BeforeProperties.ChangedProperties: {0}\n",
              properties.BeforeProperties.ChangedProperties);
            IEnumerator changedBeforeProperties =
              properties.BeforeProperties.ChangedProperties.GetEnumerator();
            i = 0;
            while (changedBeforeProperties.MoveNext())
            {
                DictionaryEntry entry = (DictionaryEntry)changedBeforeProperties.Current;
                sb.AppendFormat("[{0}]: ({1}, {2})\n", i++, entry.Key, entry.Value);
            }

            // Log the event to the list.

            ListBaseLogging.LogEvent(properties.Web, listName, properties.EventType, sb.ToString());
           
        }

        private void LogWorkflowEventProperties(SPWorkflowEventProperties properties)
        {
            // Specify the log list name.
            string listName = "WorkflowEventLogger";

            // Create the string builder object.
            StringBuilder sb = new StringBuilder();

            // Add properties that do not throw exceptions.
            sb.AppendFormat("AssociationData: {0}\n", properties.AssociationData);
            sb.AppendFormat("Cancel: {0}\n", properties.Cancel);
            sb.AppendFormat("CompletionType: {0}\n", properties.CompletionType);
            sb.AppendFormat("ErrorMessage: {0}\n", properties.ErrorMessage);
            sb.AppendFormat("EventType: {0}\n", properties.EventType);
            sb.AppendFormat("InitiationData: {0}\n", properties.InitiationData);
            sb.AppendFormat("InstanceId: {0}\n", properties.InstanceId);
            sb.AppendFormat("PostponedEvent: {0}\n", properties.PostponedEvent);
            sb.AppendFormat("ReceiverData: {0}\n", properties.ReceiverData);
            sb.AppendFormat("RedirectUrl: {0}\n", properties.RedirectUrl);
            sb.AppendFormat("RelativeWebUrl: {0}\n", properties.RelativeWebUrl);
            sb.AppendFormat("SiteId: {0}\n", properties.SiteId);
            sb.AppendFormat("Status: {0}\n", properties.Status);
            sb.AppendFormat("TerminatedByUserId: {0}\n", properties.TerminatedByUserId);
            sb.AppendFormat("WebUrl: {0}\n", properties.WebUrl);

            // Get activation properties.
            SPWorkflowActivationProperties activationProperties = properties.ActivationProperties;
            if (activationProperties != null)
            {
                sb.AppendFormat("ActivationProperties.AssociationData: {0}\n",
                  activationProperties.AssociationData);
                sb.AppendFormat("ActivationProperties.HistoryListId: {0}\n",
                  activationProperties.HistoryListId);
                sb.AppendFormat("ActivationProperties.HistoryListUrl: {0}\n",
                  activationProperties.HistoryListUrl);
                sb.AppendFormat("ActivationProperties.InitiationData: {0}\n",
                  activationProperties.InitiationData);
                sb.AppendFormat("ActivationProperties.ItemId: {0}\n",
                  activationProperties.ItemId);
                sb.AppendFormat("ActivationProperties.ItemUrl: {0}\n",
                  activationProperties.ItemUrl);
                sb.AppendFormat("ActivationProperties.ListId: {0}\n",
                  activationProperties.ListId);
                sb.AppendFormat("ActivationProperties.ListUrl: {0}\n",
                  activationProperties.ListUrl);
                sb.AppendFormat("ActivationProperties.Originator: {0}\n",
                  activationProperties.Originator);
                sb.AppendFormat("ActivationProperties.OriginatorEmail: {0}\n",
                  activationProperties.OriginatorEmail);
                sb.AppendFormat("ActivationProperties.SiteId: {0}\n",
                  activationProperties.SiteId);
                sb.AppendFormat("ActivationProperties.SiteUrl: {0}\n",
                  activationProperties.SiteUrl);
                sb.AppendFormat("ActivationProperties.TaskListId: {0}\n",
                  activationProperties.TaskListId);
                sb.AppendFormat("ActivationProperties.TaskListUrl: {0}\n",
                  activationProperties.TaskListUrl);
                sb.AppendFormat("ActivationProperties.TemplateName: {0}\n",
                  activationProperties.TemplateName);
                sb.AppendFormat("ActivationProperties.WebId: {0}\n",
                  activationProperties.WebId);
                sb.AppendFormat("ActivationProperties.WebUrl: {0}\n",
                  activationProperties.WebUrl);
                sb.AppendFormat("ActivationProperties.WorkflowId: {0}\n",
                  activationProperties.WorkflowId);

                // Add properties that might throw exceptions.
                try
                {
                    sb.AppendFormat("ActivationProperties.Context: {0}\n",
                      activationProperties.Context);
                    sb.AppendFormat("ActivationProperties.HistoryList: {0}\n",
                      activationProperties.HistoryList);
                    sb.AppendFormat("ActivationProperties.Item: {0}\n",
                      activationProperties.Item);
                    sb.AppendFormat("ActivationProperties.List: {0}\n",
                      activationProperties.List);
                    sb.AppendFormat("ActivationProperties.OriginatorUser: {0}\n",
                      activationProperties.OriginatorUser);
                    sb.AppendFormat("ActivationProperties.Site: {0}\n",
                      activationProperties.Site);
                    sb.AppendFormat("ActivationProperties.TaskList: {0}\n",
                      activationProperties.TaskList);
                    sb.AppendFormat("ActivationProperties.Web: {0}\n",
                      activationProperties.Web);
                    sb.AppendFormat("ActivationProperties.Workflow: {0}\n",
                      activationProperties.Workflow);
                }
                catch (Exception e)
                {
                    sb.AppendFormat("\nError accessing ActivationProperties property:\n\n {0}", e);
                }
            }
            else
            {
                sb.AppendFormat("ActivationProperties is null\n");
            }

            // Log the event to the list.

            ListBaseLogging.LogEvent(properties.ActivationProperties.Web, listName,
              properties.EventType, sb.ToString());
           
        }
    }
}
